﻿using MediatR;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using SystemLYH.API.Application.Command.ReservoirCommand;
using SystemLYH.Domain;
using SystemLYH.Infrstructre.Impl;
using SystemLYH.Infrstructre.Interface;
using Share;
using Microsoft.EntityFrameworkCore.Infrastructure;
using NPOI.SS.UserModel;
using static Share.ParseAPI;
using System.Text.Json.Serialization;
using System.Net.Http.Json;
using Newtonsoft.Json;

namespace SystemLYH.API.Application.Handlers.ReservoirHandlers
{
    /// <summary>
    /// 下拉框显示
    /// </summary>
    public class ReservoirSelectShowCommandHandler : IRequestHandler<ReservoirSelectShowCommand,List<Reservoir>>
    {
        IReservoirRepository _reservoirRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="reservoirRepository"></param>
        public ReservoirSelectShowCommandHandler(IReservoirRepository reservoirRepository)
        {
            _reservoirRepository= reservoirRepository;
        }

        public async Task<List<Reservoir>> Handle(ReservoirSelectShowCommand request, CancellationToken cancellationToken)
        {
            List<Warehouse> result = await GetWarehoueShow();
            var list = (from a in await _reservoirRepository.GetAllAsync()
                        join b in result on a.WarehouseID equals b.warehouseName
                        where request.WarehouseID == a.WarehouseID
                        select new Reservoir
                        {
                            ReservoirId=a.ReservoirId,
                            ReservoirName=a.ReservoirName,
                        });
            return list.ToList();
        }
        public async Task<dynamic> GetWarehoueShow()
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzMyIsInNpZCI6IjE4NjcwOTg1NDU4NTM2OTgwNDgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiIiwianRpIjoiMTYzZWM0ZTYtMTM0Yi00NmYwLWIxYmEtMDFhNjkwNWU0OWFjIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6Iui0uuiJs-W1qSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.xqb5lfaYGcOKr7lXwZY5ZnAN8gzcqYfc1z9dUKieVfI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string storage = "http://localhost:5076/api/Warehouse/WarehouseList?pageIndex=1&pageSize=1000";

            string postreonse = await cilent.GetAsync(storage);
            var json = JsonConvert.DeserializeObject<ApiResultList<Warehouse>>(postreonse);
            return json.data.plist;

        }
        /// <summary>
        /// 仓库管理
        /// </summary>
        public class Warehouse
        {
            /// <summary>
            /// 仓库ID
            /// </summary>
            [Key]
            public int warehouseID { get; set; }
            /// <summary>
            /// 仓库编号
            /// </summary>
            public string? warehouseNumber { get; set; }
            /// <summary>
            /// 仓库名称
            /// </summary>
            public string? warehouseName { get; set; }
            /// <summary>
            /// 面积
            /// </summary>
            public string? warehouseArea { get; set; }
            /// <summary>
            /// 选择仓库区域
            /// </summary>
            public string? selectWarehouseArea { get; set; }
            /// <summary>
            /// 位置编号
            /// </summary>
            public string? positionNumber { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public string? principal { get; set; }
            /// <summary>
            /// 状态
            /// </summary>
            public int warehouseState { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? warehouseRemark { get; set; }
            /// <summary>
            /// 删除标记
            /// </summary>
            public bool IsDel { get; set; }
            /// <summary>
            /// 添加人
            /// </summary>
            public string? AddName { get; set; }
            /// <summary>
            /// 添加时间
            /// </summary>
            public DateTime? AddTime { get; set; }
            /// <summary>
            /// 修改人
            /// </summary>
            public string? UpdateName { get; set; }
            /// <summary>
            /// 修改时间
            /// </summary>
            public DateTime? UpdateTime { get; set; }
            /// <summary>
            /// 删除人
            /// </summary>
            public string? DelectName { get; set; }
            /// <summary>
            /// 删除时间
            /// </summary>
            public DateTime? DelectTime { get; set; }
        }
    }
}
